package com.bjsxt.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.DictData;
import com.bjsxt.dto.DictTypeDto;
import com.bjsxt.mapper.DictDataMapper;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.DictTypeMapper;
import com.bjsxt.domain.DictType;
import com.bjsxt.service.DictTypeService;
import springfox.documentation.spring.web.json.Json;

/**
* 功能描述
*
* @author ASUS
* @date 2024/03/30  11:46
*/
@Service
public class DictTypeServiceImpl  implements DictTypeService{

    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public DataGridView listPage(DictTypeDto dictTypeDto) {
        Page<DictType> page = new Page<>(dictTypeDto.getPageNum(), dictTypeDto.getPageSize());
        LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dictTypeDto.getDictName()),DictType::getDictName,dictTypeDto.getDictName());
        wrapper.like(StringUtils.isNotBlank(dictTypeDto.getDictType()),DictType::getDictType,dictTypeDto.getDictType());
        wrapper.eq(StringUtils.isNotBlank(dictTypeDto.getStatus()), DictType::getStatus,dictTypeDto.getStatus());
        wrapper.ge(null != dictTypeDto.getBeginTime(), DictType::getCreateTime,dictTypeDto.getBeginTime());
        wrapper.le(null != dictTypeDto.getEndTime(), DictType::getCreateTime,dictTypeDto.getEndTime());
        dictTypeMapper.selectPage(page, wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public DataGridView list() {
        LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictType::getStatus, Constants.STATUS_TRUE);
        return new DataGridView(null,dictTypeMapper.selectList(wrapper));
    }

    @Override
    public Boolean checkDictTypeUnique(Long dictId, String dictType) {
        dictId = (dictId == null) ? -1L : dictId;
        LambdaQueryWrapper<DictType> qw=new LambdaQueryWrapper<>();
        qw.eq(DictType::getDictType, dictType);
        DictType sysDictType = this.dictTypeMapper.selectOne(qw);
        if(null!=sysDictType &&dictId.longValue()!=sysDictType.getDictId().longValue()){
            return true; //说明不存在
        }
        return false; //说明存在
    }

    @Override
    public int insert(DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeDto,dictType);
        dictType.setCreateBy(dictTypeDto.getSimpleUser().getUserName());
        dictType.setCreateTime(DateUtil.date());
        return dictTypeMapper.insert(dictType);
    }

    @Override
    public int update(DictTypeDto dictTypeDto) {
        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeDto,dictType);
        dictType.setUpdateBy(dictTypeDto.getSimpleUser().getUserName());
        return dictTypeMapper.updateById(dictType);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        List<Long> ids = Arrays.asList(dictIds);
        if(null != ids && ids.size() > 0){
            return dictTypeMapper.deleteBatchIds(ids);
        }else{
            return -1;
        }

    }

    @Override
    public DictType selectDictTypeById(Long dictId) {
        LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dictId != null, DictType::getDictId,dictId);
        return dictTypeMapper.selectOne(wrapper);
    }

    @Override
    public void dictCatchAsync() {
        //查询出所有可用的dicttype
        LambdaQueryWrapper<DictType> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DictType::getStatus,Constants.STATUS_TRUE);
        List<DictType> dictTypes = dictTypeMapper.selectList(wrapper1);
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        //根据字典类型查询字典数据
        dictTypes.stream().forEach(dictType -> {
            LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DictData::getDictType,dictType.getDictType());
            wrapper.eq(DictData::getStatus,Constants.STATUS_TRUE);
            wrapper.orderByAsc(DictData::getDictSort);
            List<DictData> dictDataList = dictDataMapper.selectList(wrapper);
            //转换成json串
            String json = JSONUtil.toJsonStr(dictDataList);
            valueOperations.set(Constants.DICT_REDIS_PROFIX + dictType.getDictType(),json);

        });


    }
}
